package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mmlc.inter.InterAdapter;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.responsebean.CustomerListResponseBean;
import com.mmlc.utils.ViewUtils;

import java.util.List;

/**
 * Created by Administrator on 2016/11/28.
 */
public class CustomerAdapter extends InterAdapter<CustomerListResponseBean.DataBean> {
    public CustomerAdapter(Context context, List<CustomerListResponseBean.DataBean> datas) {
        super(context, datas);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final CustomerHolder holder;
        if (convertView == null) {
            holder = new CustomerHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_customer, parent, false);
            holder.checkBox = ViewUtils.getView(convertView, R.id.checkbox);
            holder.tv_name = ViewUtils.getView(convertView, R.id.tv_name);
            holder.rl_content = ViewUtils.getView(convertView, R.id.rl_content);
            holder.tv_phone = ViewUtils.getView(convertView, R.id.tv_phone);
            convertView.setTag(holder);

        } else {
            holder = (CustomerHolder) convertView.getTag();
        }

        if (isCheck) {
            holder.checkBox.setChecked(false);
            holder.checkBox.setVisibility(View.VISIBLE);
        } else {
            holder.checkBox.setVisibility(View.GONE);
        }

        CustomerListResponseBean.DataBean customer = datas.get(position);

        if (!ViewUtils.isEmpty(customer.getCustomer_phone())) {
            holder.tv_phone.setText(customer.getCustomer_phone());
        }

        if (!ViewUtils.isEmpty(customer.getCustomer_name())) {
            holder.tv_name.setText(customer.getCustomer_name());
        }

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (customerClickListener != null) {
                    customerClickListener.onCheckChanged(buttonView, isChecked);
                }
            }
        });

        holder.rl_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isCheck) {
                    holder.checkBox.setChecked(!holder.checkBox.isChecked());
                }
                if (customerClickListener != null) {
                    customerClickListener.onClickContent(v, position, isCheck);
                }
            }
        });

        return convertView;
    }



    OnCustomerClickListener customerClickListener;

    public void setCustomerClickListener(OnCustomerClickListener customerClickListener) {
        this.customerClickListener = customerClickListener;
    }

    boolean isCheck = false;

    public void setCheck(boolean isCheck) {
        this.isCheck = isCheck;
        notifyDataSetChanged();
    }

    interface OnCustomerClickListener {
        void onCheckChanged(CompoundButton buttonView, boolean isChecked);
        void onClickContent(View v, int position, boolean isCheck);

    }

    class CustomerHolder{
        CheckBox checkBox;
        RelativeLayout rl_content;
        TextView tv_name, tv_phone;
    }

}
